package com.example.dell.newstart.cart;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dell.newstart.R;


/**
 * created by Dongkai on 2018/11/19
 * 自定义的一个 数量增减器 方便多处复用，减轻活动逻辑。
 */

public class CustomerClickCounter extends LinearLayout {
    private static final String TAG = "CustomerClickCounter";
    private ImageView iconDecrease, iconIncrease;
    private TextView numberText;

    private Integer number = 1;
    private Integer MAX_NUMBER = 9999;
    private Context context;
    private static int iconDecreaseGray;
    private static int iconDecreaseColor;


    public CustomerClickCounter(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.custom_click_counter, this, true);
        iconDecrease = findViewById(R.id.iv_decrease);
        iconIncrease = findViewById(R.id.iv_increase);
        numberText = findViewById(R.id.tv_number);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomerClickCounter);
        if (attributes != null) {
            int iconIncrease = attributes.getResourceId(R.styleable.CustomerClickCounter_icon_increase, R.drawable.increase_color);
            iconDecreaseColor = attributes.getResourceId(R.styleable.CustomerClickCounter_icon_decrease, R.drawable.decrease_color);
            iconDecreaseGray = attributes.getResourceId(R.styleable.CustomerClickCounter_icon_decrease_gray, R.drawable.decrease_gray);

            Log.d(TAG, "CustomerClickCounter: in " + iconIncrease + " iconDecrease " + iconDecrease);
            int numberTextSize = (int) attributes.getDimension(R.styleable.CustomerClickCounter_number_size, 28);
            int numberTextColor = attributes.getColor(R.styleable.CustomerClickCounter_number_color, Color.RED);
            int iconSize = (int) attributes.getDimension(R.styleable.CustomerClickCounter_icon_size, 36);


            String numberString = attributes.getString(R.styleable.CustomerClickCounter_number_str);
            if (TextUtils.isEmpty(numberString)) {
                numberString = "1";
            }
            Log.d(TAG, "CustomerClickCounter: numberTextSize " + numberTextSize);
            numberText.setText(numberString);
            if (numberTextSize == 28) {
                numberTextSize = sp2px(numberTextSize);
            }
            Log.d(TAG, "CustomerClickCounter: numberTextSize " + numberTextSize);
            numberText.getPaint().setTextSize(numberTextSize);
            numberText.setTextColor(numberTextColor);

            this.iconIncrease.setImageResource(iconIncrease);
            this.iconDecrease.setImageResource(iconDecreaseColor);


        }
        iconDecrease.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: setOnClickListener");
                if (number == 1) {
                    Toast.makeText(context, "这是最低数量了，(づ￣3￣)づ╭❤～亲", Toast.LENGTH_SHORT).show();
                } else {
                    number -= 1;
                    if (number == 1) {
                        iconDecrease.setImageResource(iconDecreaseGray);
                    }
                    if (onNumberChangeListener != null) {
                        onNumberChangeListener.onNumberChange(number);
                    }
                }
                numberText.setText(number + "");
                Log.d(TAG, "onClick: number " + number);
            }
        });
        iconIncrease.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: setOnClickListener");
                int maxNumber = MAX_NUMBER;
                if (maxNumber == number) {
                    Toast.makeText(context, "没有库存了", Toast.LENGTH_SHORT).show();
                } else {
                    number += 1;
                    if (number == 2) {
                        iconDecrease.setImageResource(iconDecreaseColor);
                    }
                    if (onNumberChangeListener != null) {
                        onNumberChangeListener.onNumberChange(number);
                    }
                }
                numberText.setText(number + "");
                Log.d(TAG, "setOnClickListener onClick: number " + number);
            }
        });
        iconDecrease.setImageResource(iconDecreaseGray);
        attributes.recycle();
    }

    public void setMaxNumber(int maxNumber) {
        MAX_NUMBER = maxNumber;
    }

    public int getMaxNumber() {
        return MAX_NUMBER;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(Integer newNumber) {
        if (newNumber < 1 || newNumber > MAX_NUMBER) {
            Toast.makeText(context, "哭，数据不对唉！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (newNumber == 1) {
            iconDecrease.setImageResource(iconDecreaseGray);
        }
        if (newNumber > 1) {
            iconDecrease.setImageResource(iconDecreaseColor);
        }
        numberText.setText(newNumber + "");
        this.number = newNumber;
    }

    private int sp2px(int spValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        float front = context.getResources().getDisplayMetrics().scaledDensity;
        Log.d(TAG, "sp2px: scale " + scale + " front " + front);
        return (int) (front * spValue + 0.5);
    }

    public interface OnNumberChangeListener {
        void onNumberChange(int number);
    }

    private OnNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

}
